COP3330-02 Object-Oriented Programming
Spring 2003
Program 5 (GUI)
Due: April 17, 2003
Objectives:
The objectives of this assignment are to get hands-on
experience:
- using
containers such as JFrame
- using
graphical components such as JButton
- adding
graphical components to containers
- controlling
the arrangement of the components using layout managers
- using
anonymous inner classes for event handling
- writing
text data to files
Overview:
You will create an application that allows the user to
create journal entries. The user will
enter a date, specify the nature of the entry, enter the text of the entry, and
save the entry to disk. The nature of
any entry is either personal or business.
Warning:
You are to follow these instructions precisely. Although you are encouraged to experiment
with other widgets, do not do so on this program. You are free to accomplish the GUI initialization in any order
that you choose. You are free to choose
your own variable names if they are not specified here.
Specifics:
You will write 2 classes:
JournalFrame and JournalApp.
Class 1: JournalFrame:
The following template should be used:
public class JournalFrame extends JFrame {
// date
label, textfield, and containing panel
private
JLabel dateLabel = new JLabel("Date: ");
private
JTextField dateField = new JTextField(20);
private JPanel
datePanel = new JPanel(new FlowLayout());
...
public
JournalFrame() {
...
}
private
void initWidgets() {
...
}
private
void saveEntry() {
...
}
} // JournalFrame
The class JournalFrame,
- Declares
all widgets as private instance variables.
- Does
all widget configuration in the initWidgets method.
- Is a
subclass of JFrame.
- Exits
the program when closed (Hint:
call setDefaultCloseOperation in
initWidgets).
- Has a
no-arg constructor:
- Calls
superclass constructor with title of window. (Title can be hard-coded string of your choosing).
- Calls
initWidgets
method.
- Has a
content pane with a BorderLayout. Hint: A reference to
the content pane of the frame can be retrieved using getContentPane().
- Contains
5 JPanels that are added directly to the content pane
using the given constraints (i.e. NORTH, CENTER,
etc).
- datePanel
(NORTH)
- Contains
a JLabel (with the text “Date:
“).
- Contains
a JTextField
- No
text initially.
- Displays
20 characters.
- Layout
is FlowLayout.
- radioPanel
(WEST)
- Contains
2 JRadioButtons.
- personalButton
(with the text “Personal”). Should be selected by default.
- businessButton
(with the text “Business”).
- Layout
is GridLayout (0 rows, 1 column).
- The
buttons are mutually exclusive.
Exactly one is selected at all times. Hint: ButtonGroup.
- statusPanel
(SOUTH)
- Contains
a JLabel (no text initially).
- Layout
is FlowLayout.
- textAreaPanel
(CENTER)
- Contains
a JTextArea.
- No
text initially.
- 10
rows by 30 columns.
- Layout
is GridLayout (0 rows, 1 column).
- buttonPanel
(EAST)
- Contains
2 JButtons
- clearButton
(with the text “Clear Journal Entry”).
- When
clicked,
- Sets
text in date field to “”.
Hint: use setText().
- Sets
text in text area to “”.
- Sets
status label to “”.
- You
must use an anonymous inner class for this!
- saveButton
(with the text “Save Journal Entry”).
- When
clicked,
- Calls
private saveEntry method.
- You
must use an anonymous inner class for this!
- Layout
is GridLayout (0 rows, 1 column).
- Has an
initWidgets
method.
- Prepares
components.
- Sets
up containment hierarchy (i.e. adds components to containers).
- Calls
the pack method.
- Has a saveEntry
method.
- If
the personal button is selected,
- File
to append to is personal.txt. (Hint: For append functionality, see available constructors for FileWriter).
- Else
- File
to append to is business.txt. Do not use absolute paths on either
of the filenames!
- Open
file and write: (You must use a
BufferedWriter!)
- Date
field contents then advance a line.
Hint: FileWriter.newLine().
- Text
area contents then advance a line.
- Close
the file.
- If
an IOException occurs,
- Set
status text to “Error: Entry could not be saved.”.
- Otherwise
- Clear
date field.
- Clear
text area.
- Set
status text to “Saved journal entry!”.
Class 2: JournalApp:
- Has a main
method.
- Instantiates
a JournalFrame.
- Sets
the frame visible.
Screenshots:
Your windows should look like these:

Figure 1
Prior to Saving

Figure 2
After Sucessful Save

Figure 3
After Unsuccessful Save

Figure 4
Panel Breakdown
Submittal:
Submittals
should follow the program specifications of
the class.
Write-up:
There
is no write-up required for this program.
Tips:
· In the examples page,
there is a class called ConsoleToFile (in the Input/Output download) that demonstrates
how to write character data to a file.
· In the examples page,
there are plenty of GUI classes (in the GUI download) to use as examples.